<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      size="small"
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="120px"
    >
      <el-form-item label="素材分组名称" prop="groupId">
        <el-select v-model="dataForm.groupId" placeholder="素材分组名称">
          <el-option
            v-for="item in groupArrays"
            :label="item.name"
            :key="item.id"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="素材" prop="url">
        <multi-upload v-model="dataForm.url"></multi-upload>
      </el-form-item>
    </el-form>

    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>


<script>
import { batchUpdateMaterial, getMaterialGroupList } from "@/api/product";
import MultiUpload from "@/components/upload/multiUpload";

export default {
  data() {
    return {
      visible: false,
      groupArrays: [],
      dataForm: {
        groupId: "",
        url: "",
      },
      dataRule: {
        groupId: [
          { required: true, message: "素材分组名称不能为空", trigger: "blur" },
        ],
        url: [{ required: true, message: "素材不能为空", trigger: "blur" }],
      },
      dataListLoading: false,
    };
  },
  components: { MultiUpload },
  activated() {},
  methods: {
    init(data) {
      this.dataForm.id = 0;
      this.dataForm.parentId = 0;

      getMaterialGroupList().then((res) => {
        if (res && res.code === 0) {
          this.groupArrays = res.data;
        }
      });

      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
      });
    },
    // 菜单树选中
    dataFormSubmit() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          batchUpdateMaterial({
            groupId: this.dataForm.groupId || 0,
            url: this.dataForm.url,
          }).then((res) => {
            if (res && res.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1000,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                },
              });
            } else {
              this.$message.error(res.msg);
            }
          });
        }
      });
    },
  },
};
</script>
